package com.guofen.controller.shlj;

import com.guofen.pojo.ShljDeodorizationtowerWaterChange1;
import com.guofen.pojo.ShljTroubleshooting;
import com.guofen.service.shlj.TroubleshootingService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RequestMapping("/shlj/troubleshooting")
@Controller
@Api(tags = "TroubleshootingController", description = "故障维修原始记录表")
public class TroubleshootingController {

    @Autowired
    private TroubleshootingService service;

    //保存
    @ApiOperation("添加记录")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Result add(@ApiParam(name="personInCharge",value="负责人",required=true) @RequestParam String personInCharge,
                      @ApiParam(name="systemName",value="系统名称",required=true) @RequestParam String systemName,
                      @ApiParam(name="equipmentName",value="设备名称",required=true) @RequestParam String equipmentName,
                      @ApiParam(name="stage",value="阶段",required=true) @RequestParam String stage,
                      @ApiParam(name="recordingPerson",value="记录人",required=true) @RequestParam String recordingPerson,
                      @ApiParam(name="timeOfFailure",value="故障发生时间",required=true) @RequestParam String timeOfFailure,
                      @ApiParam(name="faultDescription",value="故障描述",required=true) @RequestParam String faultDescription,
                      @ApiParam(name="faultReasion",value="错误原因",required=true) @RequestParam String faultReasion,
                      @ApiParam(name="faultRepairMethod",value="错误修理方法",required=true) @RequestParam String faultRepairMethod,
                      @ApiParam(name="repairStartTime",value="维修开始时间",required=true) @RequestParam String repairStartTime,
                      @ApiParam(name="repairEndTime",value="维修结束时间",required=true) @RequestParam String repairEndTime,
                      @ApiParam(name="repairPerson",value="维修人",required=true) @RequestParam String repairPerson,
                      @ApiParam(name="methodProposer",value="维修方法提出人",required=true) @RequestParam String methodProposer){
        if (service.add(personInCharge,systemName,equipmentName,stage,recordingPerson,timeOfFailure,faultDescription,
                faultReasion,faultRepairMethod,repairStartTime,repairEndTime,repairPerson,methodProposer))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("保存失败");
    }

    @ApiOperation("获取 当日 最新 一条记录")
    @RequestMapping(value = "/getlast",method = RequestMethod.GET)
    @ResponseBody
    public Result get(){
        return  ResultGenerator.genSuccessResult(service.getlast());
    }

    @ApiOperation("获取后半段还未添加的记录")
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    @ResponseBody
    public Result getList() {
        return ResultGenerator.genSuccessResult(service.getList());
    }

    @ApiOperation("添加前半部分信息")
    @RequestMapping(value = "/addFrontPart", method = RequestMethod.POST)
    @ResponseBody
    public Result addFrontPart(@ApiParam(name="personInCharge",value="负责人",required=true) @RequestParam String personInCharge,
                               @ApiParam(name="systemName",value="系统名称",required=true) @RequestParam String systemName,
                               @ApiParam(name="equipmentName",value="设备名称",required=true) @RequestParam String equipmentName,
                               @ApiParam(name="stage",value="阶段",required=true) @RequestParam String stage,
                               @ApiParam(name="recordingPerson",value="记录人",required=true) @RequestParam String recordingPerson,
                               @ApiParam(name="timeOfFailure",value="故障发生时间",required=true) @RequestParam String timeOfFailure,
                               @ApiParam(name="faultDescription",value="故障描述",required=true) @RequestParam String faultDescription) {
        return ResultGenerator.genSuccessResult(service.addFrontPart(personInCharge,systemName,equipmentName,stage,recordingPerson,timeOfFailure,faultDescription));
    }

    @ApiOperation("添加后半部分信息")
    @RequestMapping(value = "/addAfterPart", method = RequestMethod.POST)
    @ResponseBody
    public Result addAfterPart(@ApiParam(name="id",value="id",required=true) @RequestParam Integer id,
                               @ApiParam(name="faultReasion",value="错误原因",required=true) @RequestParam String faultReasion,
                               @ApiParam(name="faultRepairMethod",value="错误修理方法",required=true) @RequestParam String faultRepairMethod,
                               @ApiParam(name="repairStartTime",value="维修开始时间",required=true) @RequestParam String repairStartTime,
                               @ApiParam(name="repairEndTime",value="维修结束时间",required=true) @RequestParam String repairEndTime,
                               @ApiParam(name="repairPerson",value="维修人",required=true) @RequestParam String repairPerson,
                               @ApiParam(name="methodProposer",value="维修方法提出人",required=true) @RequestParam String methodProposer) {
        return ResultGenerator.genSuccessResult(service.addAfterPart(id, faultReasion, faultRepairMethod, repairStartTime, repairEndTime, repairPerson, methodProposer));
    }

    @ApiOperation("按主键（ID）删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteById(@ApiParam(name="id",value="id",required=true) @RequestParam Integer id) {
        return ResultGenerator.genSuccessResult(service.deleteById(id));
    }

    @ApiOperation("修改记录")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id,
                         @ApiParam(name="personInCharge",value="负责人",required=true) @RequestParam String personInCharge,
                         @ApiParam(name="systemName",value="系统名称",required=true) @RequestParam String systemName,
                         @ApiParam(name="equipmentName",value="设备名称",required=true) @RequestParam String equipmentName,
                         @ApiParam(name="stage",value="阶段",required=true) @RequestParam String stage,
                         @ApiParam(name="recordingPerson",value="记录人",required=true) @RequestParam String recordingPerson,
                         @ApiParam(name="timeOfFailure",value="故障发生时间",required=true) @RequestParam String timeOfFailure,
                         @ApiParam(name="faultDescription",value="故障描述",required=true) @RequestParam String faultDescription,
                         @ApiParam(name="faultReasion",value="错误原因",required=true) @RequestParam String faultReasion,
                         @ApiParam(name="faultRepairMethod",value="错误修理方法",required=true) @RequestParam String faultRepairMethod,
                         @ApiParam(name="repairStartTime",value="维修开始时间",required=true) @RequestParam String repairStartTime,
                         @ApiParam(name="repairEndTime",value="维修结束时间",required=true) @RequestParam String repairEndTime,
                         @ApiParam(name="repairPerson",value="维修人",required=true) @RequestParam String repairPerson,
                         @ApiParam(name="methodProposer",value="维修方法提出人",required=true) @RequestParam String methodProposer) {
        ShljTroubleshooting record = new ShljTroubleshooting();
        record.setId(id);
        record.setPersonInCharge(personInCharge);
        record.setSystemName(systemName);
        record.setEquipmentName(equipmentName);
        record.setStage(stage);
        record.setRecordingPerson(recordingPerson);
        record.setTimeOfFailure(timeOfFailure);
        record.setFaultDescription(faultDescription);
        record.setFaultReasion(faultReasion);
        record.setFaultRepairMethod(faultRepairMethod);
        record.setRepairStartTime(repairStartTime);
        record.setRepairEndTime(repairEndTime);
        record.setRepairPerson(repairPerson);
        record.setMethodProposer(methodProposer);
        return ResultGenerator.genSuccessResult(service.update(record));
    }

    @ApiOperation("查找记录")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public Result select(@ApiParam(name="pageSize",value="页容量",required=true) @RequestParam Integer pageSize,
                         @ApiParam(name="currentPage",value="当前页",required=true) @RequestParam Integer currentPage,
                         @ApiParam(name="startTime",value="开始时间") Long startTime,
                         @ApiParam(name="endTime",value="截止时间") Long endTime) {
        if (startTime == null || endTime == null)
            return ResultGenerator.genSuccessResult(service.get(pageSize, currentPage));
        else
            return ResultGenerator.genSuccessResult(service.getByTime(pageSize, currentPage, startTime, endTime));
    }
}
